//
//  HZProgressImageView.swift
//  weibo
//
//  Created by 张宏芝 on 15/9/15.
//  Copyright © 2015年 kama. All rights reserved.
//

import UIKit

class HZProgressImageView: UIImageView {

//     进度数值:0-1
    var progress:CGFloat = 0 {
        didSet{
            progressView.progress = progress
        }
    }
    
    private lazy var progressView:HZProgressView = {
        let p = HZProgressView()
        p.backgroundColor = UIColor.clearColor()
//        添加控件
        self.addSubview(p)
        p.frame = self.bounds
        return p
    }()
    
//    在imageView中,drawRect不会被调用
    override func drawRect(rect: CGRect) {
        print("执行到了这里")
    }
    
//    类中类,专供HZProgressImageView使用
    private class HZProgressView:UIView{
//        进度数值0-1
        var progress:CGFloat = 0{
            didSet{
                setNeedsDisplay()
            }
        }
        
//        rect:是bounds
        private override func drawRect(rect: CGRect) {
            print("\(rect) \(progress)")
            if progress >= 1{
                return
            }
            
//            绘制曲线
            let center = CGPoint(x: rect.width * 0.5, y: rect.height * 0.5)
            let r = min(rect.width,rect.height) * 0.5
            let start = -CGFloat(M_PI_2)
            let end = 2 * CGFloat(M_PI) * progress + start
//            添加曲线
            let path = UIBezierPath(arcCenter: center, radius: r, startAngle: start, endAngle: end, clockwise: true)
            path.addLineToPoint(center)
            path.closePath()
            UIColor(white: 0.0, alpha: 0.5).setFill()
            path.fill()
        }
    }
}



























